/*
 * $Id: NameBasedLoggerConfigurationProvider.java 165 2013-01-29 11:12:24Z vladimirow@mail.ru $
 * $HeadURL: http://ultralog.googlecode.com/svn/ultralog/trunk/src/java/com/google/code/ultralog/configuration/NameBasedLoggerConfigurationProvider.java $
 *
 * This file is a part of ultralog project (http://code.google.com/p/ultralog/).
 * Released under New BSD License.
 */
package com.google.code.ultralog.configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * An implementation of {@link LoggerConfigurationProvider} interface that
 * choose logger configurations based on logger names.
 *
 * @author Mikhail Vladimirov
 */
public class NameBasedLoggerConfigurationProvider
    implements LoggerConfigurationProvider
{
    private final LoggerConfiguration defaultLoggerConfiguation;
    private final Map <String, LoggerConfiguration> configurations;

    /**
     * Create new configuration provider with given logger name prefixes and
     * corresponding logger configurations.
     *
     * @param defaultLoggerConfiguration default logger configuration
     * @param loggerNamePrefixes logger name prefixes
     * @param loggerConfigurations logger configurations corresponding to
     *        logger name prefixes
     */
    public NameBasedLoggerConfigurationProvider (
        LoggerConfiguration defaultLoggerConfiguration,
        String [] loggerNamePrefixes,
        LoggerConfiguration [] loggerConfigurations)
    {
        if (defaultLoggerConfiguration == null)
            throw new IllegalArgumentException (
                "Default logger configuration is null");

        if (loggerNamePrefixes == null)
            throw new IllegalArgumentException (
                "Logger name prefixes is null");

        if (loggerConfigurations == null)
            throw new IllegalArgumentException (
                "Logger configurations is null");

        this.defaultLoggerConfiguation = defaultLoggerConfiguration;

        int loggerNamePrefixesCount = loggerNamePrefixes.length;
        int loggerConfigurationsCount = loggerConfigurations.length;

        if (loggerNamePrefixesCount != loggerConfigurationsCount)
            throw new IllegalArgumentException (
                "Logger name prefixes count (" + loggerNamePrefixesCount +
                ") is not the same as logger configurations count (" +
                loggerConfigurationsCount + ")");

        configurations = new HashMap <String, LoggerConfiguration> ();
        for (int i = 0; i < loggerNamePrefixesCount; i++)
        {
            String prefix = loggerNamePrefixes [i];
            LoggerConfiguration configuration = loggerConfigurations [i];

            if (prefix == null)
                throw new IllegalArgumentException (
                    "Logger name prefix #" + i + " is null");

            if (configuration == null)
                throw new IllegalArgumentException (
                    "Logger configuration #" + i + " is null");

            if (configurations.containsKey (prefix))
                throw new IllegalArgumentException (
                    "Duplicate logger name prefix: " + prefix);

            configurations.put (prefix, configuration);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public LoggerConfiguration getConfiguration (String loggerName)
    {
        if (loggerName == null)
            throw new IllegalArgumentException ("Logger name is null");

        while (!loggerName.isEmpty ())
        {
            LoggerConfiguration configuration = configurations.get (loggerName);
            if (configuration != null)
                return configuration;

            int dotIndex = loggerName.lastIndexOf ('.');
            if (dotIndex >= 0)
                loggerName = loggerName.substring (0, dotIndex);
            else loggerName = "";
        }

        return defaultLoggerConfiguation;
    }
}
